package com.cade.agriculturemall.view.personalCenter.manageAddress;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.cade.agriculturemall.R;
import com.cade.agriculturemall.base.BaseActivity;
import com.cade.agriculturemall.model.AddressEntity;
import com.cade.agriculturemall.view.personalCenter.editAddress.EditAddressActivity;
import com.cade.agriculturemall.widget.EmptyLayout;

import java.util.List;

public class ManageAddressActivity extends BaseActivity<ManageAddressContract.IPresenter> implements ManageAddressContract.IView {

    private ManageAddressAdapter mAdapter = new ManageAddressAdapter(this);
    private EmptyLayout mEmptyLayout;
    private SwipeRefreshLayout swipeRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manage_address);
        setupToolbar(true);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //loading
        mEmptyLayout.loading();
        mPresenter.downLoadData();
    }

    @Override
    public void setupView() {
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.manage_address_refreshLayout);
        RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.manage_recyclerview);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mAdapter);
        Button button = (Button) findViewById(R.id.manage_btn_add);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addAddress();
            }
        });
        mEmptyLayout = (EmptyLayout) findViewById(R.id.manage_address_empty);
        mEmptyLayout.setOnEmptyLayoutListener(new EmptyLayout.OnEmptyLayoutListener() {
            @Override
            public boolean onReloadClick(EmptyLayout layout) {
                mPresenter.downLoadData();
                return true;
            }
        });
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.downLoadData();
            }
        });
    }

    private void addAddress() {
        Intent intent = new Intent(this, EditAddressActivity.class);
        intent.putExtra("type", "2");
        startActivity(intent);
    }

    @Override
    public void setupEvent() {

    }

    @Override
    public void setupData() {
    }

    @Override
    public ManageAddressContract.IPresenter setupPresenter() {
        return new ManageAddressPresenter(this);
    }

    @Override
    public void setData(List<AddressEntity> list) {
        if (list.size()!=0) {
            mEmptyLayout.reset();
            mAdapter.setEntityList(list);
            //reset
        } else {
//            空页面
            mEmptyLayout.empty();
        }
        mAdapter.notifyDataSetChanged();
        swipeRefreshLayout.setRefreshing(false);
    }

}
